package com.crazymaker.agent.demo.aop;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.utility.JavaModule;

/**
 * Listener
 */
public class Listener implements AgentBuilder.Listener {
    public Listener(String scanPackage) {
        this.scanPackage = scanPackage;
    }

    private int count;
    private String scanPackage;

    @Override
    public void onDiscovery(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
        System.out.println(">>> " + typeName);
        if (typeName.startsWith(scanPackage)) {
            System.out.println("--- onDiscovery ---" + typeName);
        }
    }

    @Override
    public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded, DynamicType dynamicType) {
         if (typeDescription.getSimpleName().startsWith(scanPackage)) {
            System.out.println("--- onTransformation ---" + typeDescription.getSimpleName());
        }

        WeavingClassLog.INSTANCE.log(typeDescription, dynamicType);
    }

    @Override
    public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded) {
    }

    @Override
    public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) {
        if (typeName.startsWith(scanPackage)) {
            System.out.println("--- onError ---" + throwable);
        }
    }

    @Override
    public void onComplete(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {
        if (typeName.startsWith(scanPackage)) {
            System.out.println("--- onComplete ---" + typeName);
        }
    }
}
